package com.hyz.controller;

import com.github.pagehelper.Page;
import com.hyz.pojo.User;
import com.hyz.response.Result;
import com.hyz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

//    查询所有
    @GetMapping
    public Result findUserAll() {
        List<User> userAll = userService.findUserAll();
        return Result.okResult(userAll);
    }

    //条件分页查询
    @GetMapping("/page")
    public Result findPageUser(
            @RequestBody User user,
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size
    ) {
        Page<User> userPage = userService.findPageUser(user,page,size);
        return Result.okResult(userPage);
    }

    //    修改
    @PutMapping
    public Result updataUserById(@RequestBody User user) {
        boolean b = userService.updataUserById(user);
        return Result.okResult(user);
    }

    //添加
    @PostMapping
    public Result add(@RequestBody User user) {
       User user1 = userService.add(user);
        return Result.okResult(user1);
    }

    //删除
    @DeleteMapping
    public Result del(@RequestParam("id") Integer id) {
        boolean b = userService.del(id);
        return Result.okResult(200,"删除成功");
    }
}
